$(document).ready(function(){
	var waitCnt = 60;
	var t = undefined;
	//获取验证码按钮倒计时效果
	$.biginWait = function(){
		//开始倒计60秒
		if(waitCnt == 60){
			waitCnt = waitCnt-1
			$('.phone-code-get').text(waitCnt);
			$('.phone-code-get').css('text-align','center');
		}
		//如果描述大于1继续倒数
		else if (parseInt($('.phone-code-get').text()) > 1) {
			waitCnt = waitCnt-1;
			$('.phone-code-get').text(waitCnt);
		}
		//如果剩余秒数等于1将按钮设置文本为“获取验证码”，并绑定获取验证码的接口方法
		else if(parseInt($('.phone-code-get').text()) == 1){
			waitCnt = 60;
			$('.phone-code-get').text("获取验证码").css('text-align','center');
			//停止自动循环方法
			clearInterval(t);
			//点击按钮获取验证码
			$('.phone-code-get').bind('click',function(){
				$.getJSON("/"+server+"/authCodeGet?callback=?&mobile="+$('.phone').val()+requestLast,function(json){
					if(json.result == 1){
						$.popTagShow(3,"获取验证码成功",null,null,null,null,null,null,null);
						t = window.setInterval("$.biginWait()",1000);
						$('.phone-code-get').unbind("click");
					}
					else{
						$.popTagShow(1,json.msg,"为保障账号安全，请先绑定您的手机",null,null,null,null,"确定",$.popTagHide);
					}
				});				
			});			
		}
	}

	//点击获取验证码
	$('.phone-code-get').bind('click',function(){
		//判断是否满11位号码
		if($('.phone').val().length != 11){
			// $.prompt('',1,'',"请输入正确的电话号码",1,1,null);
			$.popTagShow(1,"请输入11位有效手机号码","为保障账号安全，请先绑定您的手机",null,null,null,null,"确定",$.popTagHide);
			return false;
		}
		// $.prompt('',1,'',"验证码已经发送",3,1,null);

		//请求获取验证码接口
		$.getJSON("/"+server+"/authCodeGet?callback=?&mobile="+$('.phone').val()+"&type=2"+requestLast,function(json){
			if(json.result == 1){
				$.popTagShow(3,"获取验证码成功",null,null,null,null,null,null,null);
				//进行60秒倒计时
				t = window.setInterval("$.biginWait()",1000);
				$('.phone-code-get').unbind("click");
			}
			else{
				$.popTagShow(1,json.msg,"为保障账号安全，请先绑定您的手机",null,null,null,null,"确定",$.popTagHide);
			}
		});
	});

	//点击获取语音验证码
	$('.sound-code-get').bind('click',function(){
		if($('.phone').val().length != 11){
			// $.prompt('',1,'',"请输入正确的电话号码",1,1,null);
			$.popTagShow(1,"请输入11位有效手机号码","为保障账号安全，请先绑定您的手机",null,null,null,null,"确定",$.popTagHide);
			return false;
		}
		// $.prompt('',1,'',"验证码已经发送",3,1,null);
		$.getJSON("/"+server+"/authCodeGet?callback=?&mobile="+$('.phone').val()+"&type=1"+requestLast,function(json){
			if(json.result == 1){
				$.popTagShow(3,"获取验证码成功",null,null,null,null,null,null,null);
				t = window.setInterval("$.biginWait()",1000);
				$('.phone-code-get').unbind("click");
			}
			else{
				$.popTagShow(1,json.msg,"为保障账号安全，请先绑定您的手机",null,null,null,null,"确定",$.popTagHide);
			}
		});
	});	

	//开始绑定电话
	$('.boundPhone').bind('click',function(){
		if($('.phone-code').val().length != 6){
			$.popTagShow(1,"验证码有误","请重新输入",null,null,null,null,"确定",$.popTagHide);
			return false;
		}		
		$('.boundPhone').attr("disabled","true");
		//请求绑定电话号码接口
		$.getJSON("/"+server+"/authCodeBoundPhoneForWeiXin?callback=?&access_token="+$('.access_token').val()+"&mobile="+$('.phone').val()+'&authCode='+$('.phone-code').val()+requestLast,function(json){
			if(json.result == 1){
				//将新的token set进cookie里面
				$.setCookie("access_token",json.access_token);
				access_token = json.access_token;
				window.location.href="/"+server+"/userMoney?type=2&access_token="+access_token; 
			}
			else{
				$.popTagShow(1,json.msg,"请重新输入",null,null,null,null,"确定",$.popTagHide);
			}
			$('.boundPhone').removeAttr("disabled");
		});
	})
});